package com.ymt.bpm.service.engine.bpd.impl;

import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.appboot.env.Caches;
import com.ymt.bpm.dao.MDao;
import com.ymt.bpm.dao.Mapper;
import com.ymt.bpm.model.PBpd;
import com.ymt.bpm.model.PBpdVersionWithBLOBs;
import com.ymt.bpm.service.engine.bpd.BpdRefService;
import com.ymt.bpm.service.engine.bpd.BpdVersionCacheService;
import com.ymt.bpm.service.platform.cache.CacheService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.DhxUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Johnny on 2017/4/16.
 */
@Service
public class BpdVersionCacheServiceImpl implements BpdVersionCacheService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MDao dao;

    @Autowired
    private BpdRefService brs;

    @Autowired
    private CacheService cacheService;

    @Override
    public PBpd getBasicBean(String tenantId, String bpdId) {
        String cacheKey = cacheService.asKey(Caches.BpdPrefix.BASIC_BEAN.name(), tenantId, bpdId);
        PBpd bpd = cacheService.get(Caches.BPD, cacheKey);
        if (bpd==null) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put(Const.TENANT_ID, tenantId);
            paramMap.put("bpdId", bpdId);
            bpd = dao.selectOne(Mapper.PBpdMapper, "selectSimpleByPrimaryKey", paramMap);
            if (bpd!=null) {
                cacheService.put(Caches.BPD, cacheKey, bpd);
            }
        }

        return bpd;
    }

    @Override
    public JSONObject getBpdSimple(String tenantId, String bpdId, String lang, int timezoneOffset) {
        PBpd bpd = getBasicBean(tenantId, bpdId);
        JSONObject rtn = DhxUtil.toFastJsonWithDate(bpd, lang, timezoneOffset);
        return rtn;
    }

    /*@Override
    public JSONObject getBpd(String tenantId, String bpdId, String lang, int timezoneOffset) {
        String cacheKey = cacheService.asKey(Caches.BpdPrefix.BPD.name(), tenantId, bpdId, lang, String.valueOf(timezoneOffset));
        JSONObject rtn = cacheService.get(Caches.BPD, cacheKey);
        if (rtn==null) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put(Const.TENANT_ID, tenantId);
            paramMap.put("bpdId", bpdId);
            PBpd bpd = dao.selectOne(Mapper.PBpdMapper, "selectByPrimaryKey", paramMap);
            rtn = DhxUtil.toFastJsonWithDate(bpd, lang, timezoneOffset);
            if (rtn!=null) {
                cacheService.put(Caches.BPD, cacheKey, rtn);
            }
        }
        return rtn;
    }*/

    @Override
    // @Cacheable(value= Caches.BPD, key="'getVersionJson_'+#tenantId+'_'+#bpdId+'_'+#versionid")
    public JSONObject getVersionJson(String tenantId, String bpdId, int versionid) {
        String cacheKey = cacheService.asKey(Caches.BpdPrefix.IDVER_JSON_NOEVICT.name(), tenantId, bpdId, String.valueOf(versionid));
        JSONObject rtn = cacheService.get(Caches.BPD, cacheKey);
        if (rtn==null) {
            PBpdVersionWithBLOBs key = new PBpdVersionWithBLOBs();
            if (tenantId!=null && StringUtils.isNumeric(tenantId)) {
                key.setTenantId(Integer.parseInt(tenantId));
            }
            key.setBpdId(bpdId);
            key.setVersionid(versionid);
            PBpdVersionWithBLOBs record = dao.selectOne(Mapper.PBpdVersionMapper, "selectByObj", key);
            JSONObject bpmnConfig = JSONObject.parseObject(record.getBpmnConfig());
            JSONObject otherConfig = JSONObject.parseObject(record.getOtherConfig());
            record.setBpmnConfig(null);
            record.setOtherConfig(null);
            rtn = (JSONObject)JSONObject.toJSON(record);
            rtn.put("bpmnConfig", bpmnConfig);
            rtn.put("otherConfig", otherConfig);

            if (rtn!=null) {
                cacheService.put(Caches.BPD, cacheKey, rtn);
            }
        }
        return rtn;
    }

    /*@Override
    //@Cacheable
    public JSONObject getCurrentVersionJson(String tenantId, String bpdId) {
        PBpdVersionWithBLOBs key = new PBpdVersionWithBLOBs();
        if (tenantId!=null && StringUtils.isNumeric(tenantId)) {
            key.setTenantId(Integer.parseInt(tenantId));
        }
        key.setBpdId(bpdId);
        PBpdVersionWithBLOBs record = dao.selectOne(mapperv, "selectCurrentByObj", key);
        JSONObject bpmnConfig = JSONObject.parseObject(record.getBpmnConfig());
        JSONObject otherConfig = JSONObject.parseObject(record.getOtherConfig());
        record.setBpmnConfig(null);
        record.setOtherConfig(null);
        JSONObject rtn = (JSONObject)JSONObject.toJSON(record);
        rtn.put("bpmnConfig", bpmnConfig);
        rtn.put("otherConfig", otherConfig);
        return rtn;
    }*/

    @Override
    // @Cacheable(value= Caches.BPD, key="'selectBpdDefIdByKeyVer_'+#tenantId+'_'+#bpdId+'_'+#versionid")
    public String selectBpdDefIdByKeyVer(String tenantId, String bpdId, int versionid) {
        String cacheKey = cacheService.asKey(Caches.BpdPrefix.IDVER_BPDDEFID_NOEVICT.name(), tenantId, bpdId, String.valueOf(versionid));
        String dId = cacheService.get(Caches.BPD, cacheKey);
        if (dId==null) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            if (tenantId!=null && tenantId.length()>0) {
                paramMap.put(Const.TENANT_ID, tenantId);
            }
            paramMap.put("bpdId", bpdId);
            paramMap.put("versionid", versionid);
            dId = dao.selectOne(Mapper.ActivitiMapper, "selectBpdDefIdByKeyVer", paramMap);
            if (dId!=null) {
                cacheService.put(Caches.BPD, cacheKey, dId);
            }
        }

        return dId;
    }

    @Override
    //@Cacheable(value= Caches.BPD, key="'selectBpdDefById_'+#tenantId+'_'+#processDefId")
    public JSONObject selectBpdDefById(String tenantId, String processDefId) {
        String cacheKey = cacheService.asKey(Caches.BpdPrefix.IDVER_BPDDEF_NOEVICT.name(), tenantId, processDefId);
        JSONObject rtn = cacheService.get(Caches.BPD, cacheKey);
        if (rtn==null) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            if (tenantId!=null && tenantId.length()>0) {
                paramMap.put(Const.TENANT_ID, tenantId);
            }
            paramMap.put("processDefId", processDefId);
            Map<String, Object> defMap = dao.selectOne(Mapper.ActivitiMapper, "selectBpdDefById", paramMap);
            rtn = DhxUtil.toJsonWithDateAli(defMap, Const.LANG_ZH, 0, true);
            if (rtn!=null) {
                cacheService.put(Caches.BPD, cacheKey, rtn);
            }
        }
        return rtn;
    }
}
